<?php
class TbfInput {
    //读入一个参数,并且类型是字符串,不符合要求的返回''
    static function inStr($key){
        $val = self::request($key);
       //可能是数组
        if (!is_string($val)){
            return '';
        }
        return $val;
    }

    //读入一个参数,并且类型是数值,不符合要求的返回0
    static function inNum($key){
        $val = self::request($key);
        //可能是数组
        if (!is_string($val)){
            return 0;
        }
        return intval($val);
    }
    //读入一个参数,并且类型是数组,不符合要求返回array()
    //注意此处不能保证返回的array的内部的数据结构.
    static function inArray($key){
        $val = self::request($key);
        if (!is_array($val)){
            return array();
        }
        return $val;
    }

    //必须是post请求,请在所有action前面加上,避免用户操作错误
    static function mustPost(){
        if (!TbfInput::isPost()){
            TbfError::client('http method must be POST');
            return;
        }
    }

    //读入字符串参数$key,该参数必须存在
    static function mustInStr($key){
        $value = TbfInput::inStr($key);
        if ($value===''){
            TbfError::client('require '.$key.' parameter');
            return '';
        }
        return $value;
    }

    static function mustInNum($key){
        $value = TbfInput::inNum($key);
        if ($value===0){
            TbfError::client('require '.$key.' parameter');
            return '';
        }
        return $value;
    }

    //测试请统一把参数放在 $_REQUEST 里面
    private static function request($key){
        if (isset($_POST[$key])){
            return $_POST[$key];
        }
        if (isset($_GET[$key])){
            return $_GET[$key];
        }
        if (isset($_REQUEST[$key])){
            return $_REQUEST[$key];
        }
        return '';
    }

    static function isGet(){
        return self::server('REQUEST_METHOD') === 'GET';
    }
    static function isPost(){
        return self::server('REQUEST_METHOD') === 'POST';
    }
    //获取http头
    static function header($key){
        $key = str_replace('-','_',strtoupper($key));
        return self::server('HTTP_'.$key);
    }
    static function server($key){
        if (empty($_SERVER[$key])){
            return '';
        }
        return $_SERVER[$key];
    }
    static function isSecure(){
        $https = self::server('HTTPS');
        if ($https=='on'||$https==1){
            return true;
        }
        return false;
    }
    static function isWeb(){
        return !empty($_SERVER['HTTP_HOST']);
    }
    //获取协议 http还是https
    //REQUEST_SCHEME不靠谱,找到一个不产生该数据的服务器
    static function getScheme(){
        if (self::isSecure()){
            return 'https';
        }else{
            return 'http';
        }
    }

    // 返回输入的UrlPath
    // 如果访问 http://xxx.com/abc/abc?a=1 会返回 /abc/abc
    static function getUrlPath(){
        if (empty($_SERVER['REQUEST_URI'])){
            //现在不是运行在web模式,搞不出来uri
            return null;
        }
        return parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH);
    }

    // 当前页面的uri
    // 如果访问 http://xxx.com/abc/abc?a=1 会返回 /abc/abc?a=1
    static function getUri(){
        if (empty($_SERVER['REQUEST_URI'])){
            //现在不是运行在web模式,搞不出来uri
            return null;
        }
        return $_SERVER['REQUEST_URI'];
    }

    // 使用http输入的语言代码列表和已有的语言代码列表,进行匹配,找到http输入里面第一个匹配项,
    // 返回该语言代码,如果没有找到返回null
    static function matchLanguageFromList($langCodeList){
        $langCodeMap = array();
        foreach($langCodeList as $code){
            $langCodeMap[$code] = true;
        }
        foreach(self::getLanguageList() as $code){
            if (!empty($langCodeMap[$code])){
                return $code;
            }
        }
        return null;
    }

    static function getIp(){
        if (!empty($_SERVER['HTTP_X_MOCK_IP']) && self::isTestClient()){
            return $_SERVER['HTTP_X_MOCK_IP'];
        }
        if (!empty($_SERVER['REMOTE_ADDR'])){
            return $_SERVER['REMOTE_ADDR'];
        }
        return '';
    }

    //浏览器输入的语言列表,按照输入顺序排序 '-'=>变成'_',所有字符都变成小写
    static function getLanguageList(){
        if (empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
            return array();
        }
        $languages = \TbfInput\AcceptHeader::fromString($_SERVER['HTTP_ACCEPT_LANGUAGE'])->all();
        $outputLanguages = array();
        foreach (array_keys($languages) as $lang) {
            $lang = str_replace('-','_',$lang);
            $lang = strtolower($lang);
            if (strstr($lang, '_')) {
                $codes = explode('-', $lang);
                if ($codes[0] == 'i') {
                    // Language not listed in ISO 639 that are not variants
                    // of any listed language, which can be registered with the
                    // i-prefix, such as i-cherokee
                    if (count($codes) > 1) {
                        $lang = $codes[1];
                    }
                }
            }
            $outputLanguages[] = $lang;
        }
        return $outputLanguages;
    }

    static function getPostContent(){
        return file_get_contents('php://input');
    }

    //当前进程是被测试的http客户端?(HTTP跨进程测试使用)
    static function isTestClient(){
        return !empty($_SERVER['HTTP_X_TEST_CODE']) &&
            !empty(TbfConfig::$TestCode) &&
            $_SERVER['HTTP_X_TEST_CODE']===TbfConfig::$TestCode;
    }

    //当前进程是正在跑测试的进程?(同进程测试使用)
    static function isTest(){
        return TbfConfig::$IsTesting;
    }

    private static $requstId = '';
    //php随机生成的一个请求id,当前进程不变
    static function getRequestId(){
        if (empty(self::$requstId)){
            self::$requstId = TbfRand::UniqueId();
        }
        return self::$requstId;
    }
}
